using B.S.Requst;
using MediatR;
using System;
using System.Threading;
using System.Threading.Tasks;
using B.S.Domain.InvoiceManagement;
using B.S.Infratrctrue;
using Microsoft.EntityFrameworkCore;

namespace B.S.Data.MES.API.Applications.B.S.KApplications.Command.InvoiceReceiptCommand
{
    /// <summary>
    /// 更新到票命令处理程序
    /// 处理到票信息的更新逻辑
    /// </summary>
    public class UpdateInvoiceReceiptCommandHandler : IRequestHandler<UpdateInvoiceReceiptCommand, APIRequst<bool>>
    {
        private readonly IBaseRepository<InvoiceReceipt> _invoiceReceiptRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="invoiceReceiptRepository">到票仓储</param>
        public UpdateInvoiceReceiptCommandHandler(IBaseRepository<InvoiceReceipt> invoiceReceiptRepository)
        {
            _invoiceReceiptRepository = invoiceReceiptRepository;
        }

        /// <summary>
        /// 处理更新到票的命令
        /// </summary>
        /// <param name="request">更新到票命令</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>处理结果，表示更新是否成功</returns>
        public async Task<APIRequst<bool>> Handle(UpdateInvoiceReceiptCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 验证参数
                if (request.ReceiptId <= 0)
                {
                    return new APIRequst<bool> { Code = RequstCode.失败, Msg = "到票记录ID不能为空" };
                }

                if (request.ReceiptAmount <= 0)
                {
                    return new APIRequst<bool> { Code = RequstCode.失败, Msg = "到票金额必须大于0" };
                }

                if (request.ReceiptStatus < 0 || request.ReceiptStatus > 2)
                {
                    return new APIRequst<bool> { Code = RequstCode.失败, Msg = "到票状态无效" };
                }

                // 查找现有的到票记录
                var invoiceReceipt = await _invoiceReceiptRepository.GetAll().FirstOrDefaultAsync(s => s.ReceiptId == request.ReceiptId && !s.isDel);
                if (invoiceReceipt == null)
                {
                    return new APIRequst<bool> { Code = RequstCode.失败, Msg = "到票记录不存在" };
                }

                // 更新到票记录的属性
                invoiceReceipt.ReceiptDate = request.ReceiptDate;
                invoiceReceipt.ReceiptAmount = request.ReceiptAmount;
                invoiceReceipt.ReceiptStatus = request.ReceiptStatus;
                invoiceReceipt.PaymentMethod = request.PaymentMethod;
                invoiceReceipt.TransactionNo = request.TransactionNo;
                invoiceReceipt.Remarks = request.Remarks;
                invoiceReceipt.UpdateName = request.UpdateName;
                invoiceReceipt.UpdateTime = DateTime.Now;

                // 保存更新到数据库
                var result = await _invoiceReceiptRepository.Update(invoiceReceipt);
                if (result<1)
                {
                    return new APIRequst<bool> { Code = RequstCode.失败, Msg = "更新到票记录失败" };
                }

                return new APIRequst<bool>
                {
                    Code = RequstCode.成功,
                    Msg = "更新到票记录成功",
                    Data = true
                };
            }
            catch (Exception ex)
            {
                // 记录异常并返回失败结果
                return new APIRequst<bool>
                {
                    Code = RequstCode.失败,
                    Msg = $"更新到票记录失败：{ex.Message}"
                };
            }
        }
    }
}